package com.regis.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.regis.comment.uilts.ThreadDataUtil;
import com.regis.entity.db.Category;
import com.regis.entity.vo.ResultVo;
import com.regis.service.CategoryService;
import com.regis.service.impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 计算机系 ITAEM 纱雾休比我老婆
 * @Description 分类管理接口
 * @date 2022/7/29 0:19
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CacheManager cacheManager;

    /**
     * 新增分类
     * @param category 分类对象
     * @return
     */
    @CacheEvict(value = "category",allEntries = true)
    @PostMapping()
    public ResultVo<String> save(@RequestBody Category category){
        categoryService.save(category);
        return ResultVo.success("添加成功");
    }

    /**
     * 分页查询
     * @param page 第几页
     * @param pageSize 每页的条数
     * @return
     */
    @Cacheable(value = "category",key = "#page + '_' + #pageSize",unless = "#result.data == null")
    @GetMapping("/page")
    public ResultVo<Page<Category>> page(int page, int pageSize){
        //分页构造器
        Page<Category> pageInfo = new Page<>(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);

        //进行分页查询
        categoryService.page(pageInfo,queryWrapper);
        return ResultVo.success(pageInfo);
    }

    /**
     * 根据id删除分类
     * @param ids 分类id
     * @return
     */
    @CacheEvict(value = "category",allEntries = true)
    @DeleteMapping
    public ResultVo<String> delete(Long ids){
        return categoryService.remove(ids);
    }

    /**
     * 修改分类信息
     * @param category
     * @return
     */
    @CacheEvict(value = "category",allEntries = true)
    @PutMapping
    public ResultVo<String> update(@RequestBody Category category){
        categoryService.updateById(category);
        return ResultVo.success("更新成功");
    }

    /**
     * 根据条件查询分类数据
     * @param category
     * @return
     */
    @Cacheable(value = "category",key = "#category.toString()",unless = "#result.data == null")
    @GetMapping("/list")
    public ResultVo<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(category.getType() != null,Category::getType,category.getType());
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = categoryService.list(queryWrapper);
        return ResultVo.success(list);
    }
}
